package com.heima.wemedia.controller.v1;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.WmLoginDto;
import com.heima.wemedia.service.WmUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author shitou
 * @className WmUserLoginController
 * @description: 自媒体用户登录
 */
@RestController
@RequestMapping("/login")
@Slf4j
public class WmUserLoginController {

    @Resource
    private WmUserService wmUserService;

    /**
     * 用户登录
     *
     * @param wmLoginDto
     * @return
     */
    @PostMapping("/in")
    public ResponseResult login(@RequestBody WmLoginDto wmLoginDto) {
        log.info("自媒体用户登录--start");
        // 校验 账号与密码
        if (StringUtils.isBlank(wmLoginDto.getName()) || StringUtils.isBlank(wmLoginDto.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 账户不能包含特殊字符
        String validPattern = "[`~!@#$%^&*()+=|{}':;',\\\\[\\\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
        Matcher matcher = Pattern.compile(validPattern).matcher(wmLoginDto.getName());
        if (matcher.find()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_NAME);
        }

        return wmUserService.login(wmLoginDto);
    }

}
